// 创建banner路由
const express = require('express');
const r = express.Router();
// 引入数据库池
const pool = require('../pool');

// 轮播图添加接口
// http://127.0.0.1:8888/v1/banner/all
r.get('/all',(req,res,next)=>{
  pool.query('select * from hqyc_banner',(err,result)=>{
    if(err){
      next(err)
      return
    }
    console.log(result);
    result.length>0?res.send({code:1,msg:'轮播图列表查询成功',data:result}):res.send({code:0,msg:'轮播图列表查询失败',data:result});
  })
})

// 轮播图查询接口
// http://127.0.0.1:8888/v1/banner/allpic
r.get('/allpic',(req,res,next)=>{
  let obj = req.query;
  // console.log(obj);
  pool.query('select * from hqyc_banner where item_name=?',[obj.name],(err,result)=>{
    if(err){
      next(err)
      return
    }
    // console.log(result);
    result.length>0?res.send({code:1,msg:'轮播图列表查询成功',data:result}):res.send({code:0,msg:'轮播图列表查询失败',data:result});
  })
})

// 精彩必玩查询接口
// http://127.0.0.1:8888/v1/banner/play
r.get('/play',(req,res,next)=>{
  let obj = req.query;
  // console.log(obj);
  pool.query('select * from hqyc_play where topicname=?',[obj.name],(err,result)=>{
    if(err){
      next(err)
      return
    }
    // console.log(result);
    result.length>0?res.send({code:1,msg:'列表查询成功',data:result}):res.send({code:0,msg:'列表查询失败',data:result});
  })
})

// 餐饮购物查询接口
// http://127.0.0.1:8888/v1/banner/eat
r.get('/eat',(req,res,next)=>{
  let obj = req.query;
  // console.log(obj);
  pool.query('select * from hqyc_eat where topicname=?',[obj.name],(err,result)=>{
    if(err){
      next(err)
      return
    }
    // console.log(result);
    result.length>0?res.send({code:1,msg:'列表查询成功',data:result}):res.send({code:0,msg:'列表查询失败',data:result});
  })
})


module.exports = r;